home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10164 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: flute.aix.calpoly.edu!not-for-mail
  2. From: mporcell@flute.aix.calpoly.edu (Michael Anthony Porcelli)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can copy constructor and operator= share code?
  5. Date: 6 Mar 1996 05:18:41 -0800
  6. Organization: California Polytechnic State University, San Luis Obispo
  7. Message-ID: <4hk3bh$1j4s@flute.aix.calpoly.edu>
  8. References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <199603040709.a35476@iz.maus.de>
  9. NNTP-Posting-User: mporcell@flute.aix.calpoly.edu
  10.  
  11. Here's a style tip from a senior level C++/OO class on exactly this topic.  
  12. In fact our teacher *required* this for all of our classes and frankly I
  13. believe this is a GREAT C++ tip for good OO/SoftEng.
  14.  
  15. Every class should have 2 private/protected member functions called Delete
  16. and Duplicate (or something to that effect).  Duplicate should take a class&
  17. (just like the copy constructor).  Now, code re-use is easy, watch...
  18.  
  19. X(X& x) 
  20.  { Duplicate(x); } //one-statement, in-line copy constructor.
  21.  
  22. operator= (X& x) 
  23.  { Delete(); Duplicate(x); } //two-statement, in-line op=
  24.  
  25. ~X() 
  26.  { Delete(); } //one-statement, in-line destructor.
  27.  
  28. Pretty nifty, eh?  I believe that all class design should be done this way.
  29.  
  30. -Mike
  31.  
  32.  
  33.  
  34.  
  35.